package studentinformationmanagementsystem.service.impl;

import studentinformationmanagementsystem.dao.*;
import studentinformationmanagementsystem.dao.impl.*;
import studentinformationmanagementsystem.entity.*;
import studentinformationmanagementsystem.service.ClassScheduleService;
import studentinformationmanagementsystem.service.CourseService;
import studentinformationmanagementsystem.utils.TimeCycle;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 苗庆
 * @version 1.0
 */
public class ClassScheduleServiceImpl implements ClassScheduleService {


    //课程表
    ClassScheduleDao classScheduleDao = new ClassScheduleDaoImpl();

    //临时班级表
    TemporaryClassTableDao temporaryClassTableDao = new TemporaryClassTableDaolmpl();

    //学生表
     StudentDao studentDao = new StudentDaoImpl();

     //必修课表
     RequiredCourseDao requiredCourseDao = new RequiredCourseDaoImpl();

     //教师任课表
    TeachersSchedulesDaolmpl teachersSchedulesDaolmpl = new TeachersSchedulesDaolmpl();

    //课程
    CourseDao courseDao = new CourseDaolmpl();

    //学生选课记录表
    TakeACourseDao takeACoureDao = new TakeACoureDaolmpl();

    /***
     *
     * @param classSchedule
     * @return
     *
     * 业务逻辑思路：
     * 1.首先先判断传进来的是选修课还是必修课
     *必修课：
     * 2.如果是必修课的话，根据上课对象和学生专业筛选出来是必修课对象，拿到学生id
     * 3.使用教师id和课程id建立一个教师任课表的关系。
     * 4.使用教师任课表id和学生id建立必修课表。
     *
     * 选课课：
     * 使用建立的教师任课表id和上课人数限，在临时选课表中建立关系
     *
     */
    @Override
    public Map<String, String> insert(ClassSchedule classSchedule) {
        //定义个map用来装返回给上一级的判断信息
        Map<String,String> map=new HashMap<>();


        //添加课程表信息
        int insert2 = classScheduleDao.insert(classSchedule);
        ClassSchedule classSchedule1 = classScheduleDao.queryById01(classSchedule);


        //建立教师任课表关系
        TeachersSchedules teachersSchedules = new TeachersSchedules(null,classSchedule.getCourseid(),classSchedule.getTeacherId(),classSchedule1.getClassScheduleId(),0);


        //添加教师任课表关系
        int insert = teachersSchedulesDaolmpl.insert(teachersSchedules);

        TeachersSchedules teachersSchedules1=null;
        //如果添加成功的话使用教师id和课程id查询教师任课表拿到教师任课表的id
        if (insert>0){
             teachersSchedules1 = teachersSchedulesDaolmpl.queryBycourseidAndTeacherid(teachersSchedules);
        }


        //1.首先先判断传进来的是选修课还是必修课

        //选修课
        if (classSchedule.getStidentmajor().equals("1") && classSchedule.getAttendClassObject().equals("1")){
            //选课临时表
            TemporaryClassTable temporaryClassTable = new TemporaryClassTable(null,teachersSchedules1.getTeachersschedulesid(),50,0);
            int insert1 = temporaryClassTableDao.insert(temporaryClassTable);

            if (insert1>0){
                    map.put("code","0");   //"选修课表添加成功"
                    return map;
            }else {
                map.put("code","1");   //"选修课表添加失败"
                return map;
            }

        }


        //    必修课：根据上课对象和学生专业筛选出来是必修课对象，拿到学生id
        //    * 使用教师任课表id和学生id建立必修课表。

            //首先需要使用传进来的大几学生对象，计算机其入学时间

        //定义一个学生的入学时间
         String AdmissionksDate=null;
         String AdmissionjsDate=null;

        Date date = new Date();//获取当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        if (classSchedule.getAttendClassObject()!=null && !classSchedule.getAttendClassObject().equals("1")){

            if (classSchedule.getAttendClassObject().equals("大一")){

                calendar.add(Calendar.YEAR, -1);//当前时间减去一年，即一年前的时间

                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-01-31");
                AdmissionksDate = fmt.format(calendar.getTime());


                java.sql.Date time = null;
                try {
                    time = TimeCycle.time(AdmissionksDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-12-31");
                AdmissionjsDate = fmt1.format(time);

            }else if (classSchedule.getAttendClassObject().equals("大二")){
                calendar.add(Calendar.YEAR, -2);//当前时间减去一年，即一年前的时间

                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-01-31");
                AdmissionksDate = fmt.format(calendar.getTime());


                java.sql.Date time = null;
                try {
                    time = TimeCycle.time(AdmissionksDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-12-31");
                AdmissionjsDate = fmt1.format(time);
            }else if (classSchedule.getAttendClassObject().equals("大三")){

                calendar.add(Calendar.YEAR, -3);//当前时间减去一年，即一年前的时间

                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-01-31");
                AdmissionksDate = fmt.format(calendar.getTime());


                java.sql.Date time = null;
                try {
                    time = TimeCycle.time(AdmissionksDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-12-31");
                AdmissionjsDate = fmt1.format(time);

            }else {
                calendar.add(Calendar.YEAR, -4);//当前时间减去一年，即一年前的时间

                SimpleDateFormat fmt = new SimpleDateFormat("yyyy-01-31");
                AdmissionksDate = fmt.format(calendar.getTime());


                java.sql.Date time = null;
                try {
                    time = TimeCycle.time(AdmissionksDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                SimpleDateFormat fmt1 = new SimpleDateFormat("yyyy-12-31");
                AdmissionjsDate = fmt1.format(time);
            }

        }

        boolean falg=false;


        //拿到了符号条件的学生
        List<Student> students = studentDao.queryAdmissionDate(AdmissionksDate, AdmissionjsDate, classSchedule.getStidentmajor());

        //使用教师任课表id和学生id
        for (int i = 0; i <students.size() ; i++) {
            Student student = students.get(i);
            RequiredCourse requiredCourse = new RequiredCourse(null,student.getStudid(),teachersSchedules1.getTeachersschedulesid(),"0",0);
            int insert1 = requiredCourseDao.insert(requiredCourse);
            if (insert1>0){
                falg=true;
            }else {
                falg=false;
            }

        }

        if (falg){
                map.put("code","2");   //"必修课表添加成功"
                return map;
        }else {
            map.put("code","3");   //"必修课表添加失败"
            return map;
        }
    }

    /**
     *
     * @return
     */
    @Override
    public List<ClassSchedule> queryAll() {
        List<ClassSchedule> classSchedules = classScheduleDao.queryAll();
        return classSchedules;
    }


    /**
     * 根据课表id进行删除
     * @param id
     * @return
     * 1.删除当前课程，判断当前删除时间是不是小于开始时间，如果小于就直接删除，如果大于开始时间就直接返回
     * 2.通过课程id先判断这个课程是选修课，还是必修课，如果是选修课需要删除教师任课表，临时选课表，如果是必修课需要删除教师任课表和学生必修课表
     * 3.如果是直接删除的话，先通过课程id去教师任课表中，查询教师任课表的记录。
     */
    @Override
    public Map<String, String> deleteClassSchedule(Integer id) throws ParseException {

        //定义个map用来装返回给上一级的判断信息
        Map<String,String> map=new HashMap<>();


        ClassSchedule classSchedule1 = new ClassSchedule();
        classSchedule1.setClassScheduleId(id);

        //使用id查询这个课程表信息
        ClassSchedule classSchedule = classScheduleDao.queryById(classSchedule1);


        Course course = new Course();
        course.setCourseid(classSchedule.getCourseid());

        //获得课程信息
        Course course1 = courseDao.queryById(course);


        Date date = new Date();

        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

        String AdmissionksDate = fmt.format(date.getTime());

        //当前时间
        Date parse = fmt.parse(AdmissionksDate);

        //课程开始时间
        java.sql.Date time = TimeCycle.time(classSchedule.getCurriculumTime());



        if (parse.before(time)){

            //查询教师任课表的这个记录，获取教师任课表id
            TeachersSchedules teachersSchedules = new TeachersSchedules();
            teachersSchedules.setClassScheduleId(classSchedule.getClassScheduleId());
            TeachersSchedules teachersSchedules1 = teachersSchedulesDaolmpl.queryByClassSchedule(teachersSchedules);


            //删除学生必修课表记录
            if (course1.getType().equals("必修课")){

                RequiredCourse requiredCourse = new RequiredCourse();
                requiredCourse.setTeachersschedulesid(teachersSchedules1.getTeachersschedulesid());

                //查询必修课信息
                List<RequiredCourse> requiredCourses = requiredCourseDao.queryByRequiredCourse(requiredCourse);

                //删除必修课信息
                for (int i = 0; i <requiredCourses.size() ; i++) {
                    RequiredCourse requiredCourse1 = requiredCourses.get(i);
                    int delete = requiredCourseDao.delete(requiredCourse1);
                }

            }else {
                //查询临时选课表信息
                TemporaryClassTable temporaryClassTable = new TemporaryClassTable();
                temporaryClassTable.setTeachersschedulesid(teachersSchedules1.getTeachersschedulesid());

                //查询临时选课表信息
                TemporaryClassTable temporaryClassTable1 = temporaryClassTableDao.queryByTeachersschedulesid(temporaryClassTable);

                //删除临时选课表信息
                int delete = temporaryClassTableDao.delete(temporaryClassTable1);


                //删除学生的选课记录
                TakeACourse takeACourse = new TakeACourse();
                takeACourse.setTemporaryclasstableid(temporaryClassTable1.getTemporaryclasstableid());

                List<TakeACourse> takeACourses = takeACoureDao.queryBytemporaryclasstableId(takeACourse);
                if (takeACourses.size()!=0){

                    for (int i = 0; i <takeACourses.size() ; i++) {
                        TakeACourse takeACourse1 = takeACourses.get(i);
                        int delete1 = takeACoureDao.delete(takeACourse1);
                    }

                }


            }

            //删除教师任课表
            int delete = teachersSchedulesDaolmpl.delete(teachersSchedules1);

            //删除课程表记录
            int delete1 = classScheduleDao.delete(classSchedule);

            if (delete1>0){
                map.put("code","1");   //"课表删除成功"
                return map;
            }else {
                map.put("code","2");   //"课表删除失败"
                return map;
            }

        }else {
            map.put("code","0");   //"此课程已经开始上课，无法进行删除"
            return map;
        }

    }


    @Override
    public ClassSchedule queryAllid(String classScheduleId) {
        ClassSchedule classSchedule = new ClassSchedule();
        classSchedule.setClassScheduleId(Integer.valueOf(classScheduleId));
        ClassSchedule classSchedule1 = classScheduleDao.queryById(classSchedule);
        return classSchedule1;
    }


    @Override
    public Boolean reviseClassScheduleid(ClassSchedule classSchedule) {
        boolean falg=false;
        int i = classScheduleDao.update01(classSchedule);
        if (i>0){
            falg=true;
        }
        return falg;
    }
}
